﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Resources;
using EHTWebsite.Code;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Security;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.BLL;
using System.Drawing;
using System.Drawing.Drawing2D;


namespace EHTWebsiteProject.Administrator
{
    public partial class EHTMediaUI : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private Int32 BannerID = HangSo.DEFAULT_ID;
        private String message = "";       
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                try
                {
                    BannerID = Int32.Parse(Request.QueryString["BannerID"]);
                }
                catch
                {

                }

                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if (message != "")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                if (BannerID != -1)
                {                    
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Edit_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Edit_Other;
                    QuangCao theQuangCao = QuangCaoProvider.GetQuangCaoByQuangCaoID(BannerID);
                    txtTenBanner.Text = theQuangCao.GhiChuVN;
                    txtTenBannerOther.Text = theQuangCao.GhiChuOther;
                    txtThuTu.Text = theQuangCao.NumberOrder.ToString();
                    txtUrl.Text = theQuangCao.Url.ToString();
                    ddTypeBaner.SelectedValue = theQuangCao.TypeBanner.Trim();
                    Imgbanner.ImageUrl = "~/ImageDatas/Banner/"+theQuangCao.BannerUrl;
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    ViewState["BannerID"] = BannerID;
                    ViewState["ImageUrl"] = theQuangCao.BannerUrl;
                }
                else
                {
                    ViewState["action"] = HangSo.ACTION_ADD;
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Add_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Add_Other;
                }
            }
        }


        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbApply.Text = rm.GetString("lbApply", ci);
            lbbannerManager.Text = rm.GetString("lbbannerManager", ci);            
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbSave.Text = rm.GetString("lbSave", ci);
            lbTenBaner.Text = rm.GetString("lbTenBaner", ci);
            lbTenBannerOther.Text = rm.GetString("lbTenBannerOther", ci);
            lbThuTu.Text = rm.GetString("lbThuTu", ci);
            lbHinhAnh.Text = rm.GetString("lbHinhAnh", ci);
            lbLoaiBanner.Text = rm.GetString("lbLoaiBanner", ci); 
        }

        public Boolean ValidateData()
        {
            Boolean flag = true;

            if (txtTenBanner.Text == "")
            {
                if (IsVN == true)
                    message = HangSo.check_VN;
                else
                    message = HangSo.check_Other;
                flag = false;
            }
            if (txtTenBannerOther.Text == "")
            {
                if (IsVN == true)
                    message = HangSo.check_VN;
                else
                    message = HangSo.check_Other;
                flag = false;
            }
            try
            {
                Int32.Parse(txtThuTu.Text);
            }
            catch
            {
                flag = false;
            }

            return flag;
        }


        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 BannerOutID = 0;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                QuangCao theQuangCao = new QuangCao();
                theQuangCao.GhiChuOther = txtTenBannerOther.Text;
                theQuangCao.GhiChuVN = txtTenBanner.Text;
                theQuangCao.NumberOrder = Int32.Parse(txtThuTu.Text);
                theQuangCao.PhanVungID = 1;
                theQuangCao.TypeBanner = ddTypeBaner.SelectedValue;
                theQuangCao.Url = txtUrl.Text;

                if (this.FileUploadBanner.HasFile)
                {
                    String fileName = FileUploadBanner.PostedFile.FileName;
                    String pathAvatars = Server.MapPath("~/");
                    String fileType = Path.GetExtension(fileName);
                    String FileNewName = DateTime.Now.Ticks.ToString() + fileType;
                    try
                    {
                        System.Drawing.Image oImg = System.Drawing.Image.FromStream(FileUploadBanner.PostedFile.InputStream);
                        if (Int32.Parse(oImg.Width.ToString()) > 1000)
                        {


                            int thumbWidth = 1000;
                            int thumbHeight = (Int32.Parse(oImg.Height.ToString()) * thumbWidth) / (Int32.Parse(oImg.Width.ToString()));
                            System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);
                            Graphics oGraphic = Graphics.FromImage(oThumbNail);
                            oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
                            oGraphic.DrawImage(oImg, oRectangle);
                            oThumbNail.Save(pathAvatars + "\\ImageDatas\\Banner\\" + FileNewName);
                            theQuangCao.BannerUrl = FileNewName;
                            oImg.Dispose();
                        }

                        else
                        {

                            this.FileUploadBanner.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Banner//" + FileNewName);
                            theQuangCao.BannerUrl = FileNewName;
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error: " + ex.Message);
                    }
                }
                else
                {
                    if (action != HangSo.ACTION_ADD)
                    {
                        theQuangCao.BannerUrl = ViewState["ImageUrl"].ToString();
                    }
                    else
                    {
                        theQuangCao.BannerUrl = "noImage.gif";
                    }

                }
                if (action == HangSo.ACTION_ADD)
                {
                    theQuangCao.BannerID = -1;
                    BannerOutID = QuangCaoProvider.IUQuangCao(theQuangCao, true);
                }
                else
                {
                    theQuangCao.BannerID = Int32.Parse(ViewState["BannerID"].ToString());
                    BannerOutID = QuangCaoProvider.IUQuangCao(theQuangCao, false);
                }

                Response.Redirect("EHTMedia.aspx");
            }
        }

        protected void ImgApply_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 BannerOutID = 0;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                QuangCao theQuangCao = new QuangCao();
                theQuangCao.GhiChuOther = txtTenBannerOther.Text;
                theQuangCao.GhiChuVN = txtTenBanner.Text;
                theQuangCao.NumberOrder = Int32.Parse(txtThuTu.Text);
                theQuangCao.PhanVungID = 1;
                theQuangCao.TypeBanner = ddTypeBaner.SelectedValue;
                theQuangCao.Url = txtUrl.Text;
                if (this.FileUploadBanner.HasFile)
                {
                    String fileName = FileUploadBanner.PostedFile.FileName;
                    String pathAvatars = Server.MapPath("~/");
                    String fileType = Path.GetExtension(fileName);
                    String FileNewName = DateTime.Now.Ticks.ToString() + fileType;
                    try
                    {
                        System.Drawing.Image oImg = System.Drawing.Image.FromStream(FileUploadBanner.PostedFile.InputStream);
                        if (Int32.Parse(oImg.Width.ToString()) > 1000)
                        {


                            int thumbWidth = 1000;
                            int thumbHeight = (Int32.Parse(oImg.Height.ToString()) * thumbWidth) / (Int32.Parse(oImg.Width.ToString()));
                            System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);
                            Graphics oGraphic = Graphics.FromImage(oThumbNail);
                            oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
                            oGraphic.DrawImage(oImg, oRectangle);
                            oThumbNail.Save(pathAvatars + "\\ImageDatas\\Banner\\" + FileNewName);
                            theQuangCao.BannerUrl = FileNewName;
                            oImg.Dispose();
                        }

                        else
                        {

                            this.FileUploadBanner.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Banner//" + FileNewName);
                            theQuangCao.BannerUrl = FileNewName;
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error: " + ex.Message);
                    }
                }
                else
                {
                    if (action != HangSo.ACTION_ADD)
                    {
                        theQuangCao.BannerUrl = ViewState["ImageUrl"].ToString();
                    }
                    else
                    {
                        theQuangCao.BannerUrl = "noImage.gif";
                    }

                }
                if (action == HangSo.ACTION_ADD)
                {
                    theQuangCao.BannerID = -1;
                    BannerOutID = QuangCaoProvider.IUQuangCao(theQuangCao, true);
                }
                else
                {
                    theQuangCao.BannerID = Int32.Parse(ViewState["BannerID"].ToString());
                    BannerOutID = QuangCaoProvider.IUQuangCao(theQuangCao, false);
                }
                String str = "";
                if (IsVN == true)
                    str = HangSo.App_VN;
                else
                    str = HangSo.App_Other;
                Response.Redirect("EHTMediaUI.aspx?BannerID="+BannerOutID+"&&Message="+str);
            }
        }
    }
}